import 'dart:math';

import 'package:flutter/material.dart';

import 'db/db_help.dart';
import 'model/work_record.dart';

class WorkRecordListState with ChangeNotifier {
  bool _isLoading = true;
  bool _loadMore = true;
  int _start = 0;
  List<WorkRecord> _list = [];

  WorkRecordListState() {
    _queryWorkRecordList();
  }

  bool get loadMore => _loadMore;

  bool get isLoading => _isLoading;

  List<WorkRecord> get list => _list;

  set loadMore(bool value) {
    _loadMore = value;
    notifyListeners();
  }

  int len() {
    return _list.length;
  }

  Future<void> loadMoreData() async {
    final db = await dbHelp.database;
    final list = await queryWorkRecord(db, start: _start);
    if (list.isEmpty) {
      _loadMore = false;
    } else {
      _start += 12;
      _list = list;
    }
    notifyListeners();
  }

  Future<void> _queryWorkRecordList() async {
    print("query workRecord");
    final db = await dbHelp.database;
    final list = await queryWorkRecord(db);
    if (list.isNotEmpty) {
      print("_queryWorkRecordList: ${list}");
      _list.addAll(list);
    } else {
      print("_queryWorkRecordList empty retry...");
      _queryWorkRecordList();
    }
    _isLoading = false;
    notifyListeners();
  }
}
